package CocoFood.servlet.cms;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public abstract class WebServletCms extends HttpServlet {

    protected abstract void processRequest(HttpServletRequest request, HttpServletResponse response);

    protected void forward(String url, HttpServletRequest request, HttpServletResponse response) {
        try {
            RequestDispatcher requestDispatcher = request.getRequestDispatcher(url);
            requestDispatcher.forward(request, response);
        } catch (Exception ex) {
            ex.printStackTrace(System.out);

        }
    }

    protected void redirect(String url, HttpServletResponse response) {
        try {
            response.sendRedirect(url);
        } catch (Exception ex) {
            ex.printStackTrace(System.out);

        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) {
        try {
            HttpSession session = request.getSession();
            if (session.getAttribute("tisoUserTO") == null) {
                response.sendRedirect("/CMSAdmin");
                return;
            } else {
                if (checkPermission(session, request)) {
                    processRequest(request, response);
                } else {
                    forward("/cms/permission.jsp", request, response);
                    return;
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace(System.out);
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) {
        try {
            HttpSession session = request.getSession();
            if (session.getAttribute("tisoUserTO") == null) {
                response.sendRedirect("/CMSAdmin");
                return;
            } else {
                if (checkPermission(session, request)) {
                    processRequest(request, response);
                } else {
                    forward("/cms/permission.jsp", request, response);
                    return;
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace(System.out);
        }
    }

    protected String getCurrentUrl(HttpServletRequest req, boolean withQueryString) {
        String reqUrl = req.getRequestURL().toString();
        String queryString = req.getQueryString();
        if (withQueryString && queryString != null) {
            reqUrl += "?" + queryString;
        }
        return reqUrl;
    }

    protected boolean checkPermission(HttpSession session, HttpServletRequest request) {
        boolean result = false;
        if (session.getAttribute("tisoUserTO") != null) {
            int sys_level = 0;
            //LEVEL ADMIN 1, EDITOR 2, SEO 3
            if (sys_level == 1) {
                result = true;
            } else if (sys_level == 2) {
            } else if (sys_level == 3) {
            } else if (sys_level == 4) {
            }
        }
        return result;
    }
}
